﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace HexTile
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private HexTable m_table = null;

        private void OnTableChanged()
        {
            int hCount, vCount;
            try
            {
                hCount = Int32.Parse(hCountBox.Text);
                vCount = Int32.Parse(vCountBox.Text);
            }
            catch (Exception)
            {
                return;
            }

            canvas1.Children.Clear();
            if (canvas1.ActualWidth > 0 && canvas1.ActualHeight > 0)
            {
                if (hCount > 2 && vCount > 2 && hCount < 1000 && vCount < 1000)
                {
                    m_table = new HexTable(hCount, vCount);
                    m_table.SetRectangle(canvas1.ActualWidth, canvas1.ActualHeight);
                    List<Polyline> lines = m_table.CreateLines();
                    foreach (Polyline line in lines)
                    {
                        line.Stroke = Brushes.SteelBlue;
                        line.StrokeThickness = 0.01;
                        canvas1.Children.Add(line);
                    }
                }
            }
        }

        private void OnVCountChanged(object sender, TextChangedEventArgs e)
        {
            OnTableChanged();
        }

        private void OnHCountChanged(object sender, TextChangedEventArgs e)
        {
            OnTableChanged();
        }

        private void OnDrawPanelSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (m_table != null)
                m_table.SetRectangle(canvas1.ActualWidth, canvas1.ActualHeight);
        }

        private void OnCanvasBtnUp(object sender, MouseButtonEventArgs e)
        {
            Point pt = e.GetPosition(canvas1);
            int x = -1, y = -1;
            m_table.PointToHex(pt.X, pt.Y, ref x, ref y);
            MessageBox.Show("Selected (" + (x + 1) + ", " + (y + 1) + ")");
        }
    }
}
